//package com.br.cloud.br.common.core.oauth;
//
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.codehaus.jackson.map.ObjectMapper;
//import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
//import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
//import org.springframework.security.oauth2.client.registration.ClientRegistration;
//import org.springframework.security.oauth2.core.OAuth2AccessToken;
//import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
//import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange;
//import org.springframework.util.LinkedMultiValueMap;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.HashMap;
//import java.util.LinkedHashSet;
//import java.util.Map;
//import java.util.Set;
//
///**
// * @author yzp
// * @date 2021/2/18 16:39
// */
//@Slf4j
//public class HaierOAuth2AccessTokenResponseClient implements OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> {
//    private static final RestTemplate restTemplate = new RestTemplate();
//
//    @SneakyThrows
//    @Override
//    public OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationGrantRequest) {
//        ClientRegistration clientRegistration = authorizationGrantRequest.getClientRegistration();
//        OAuth2AuthorizationExchange oAuth2AuthorizationExchange = authorizationGrantRequest.getAuthorizationExchange();
//        //根据海尔的接口文档,获取access_token参数
//        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
//        params.set("client_id", clientRegistration.getClientId());
//        //params.set("client_secret", clientRegistration.getClientSecret());
//        params.set("client_secret", "09bb3743b7474ba2ada887e65899cf2a");
//        params.set("code", oAuth2AuthorizationExchange.getAuthorizationResponse().getCode());
//        params.set("redirect_uri", oAuth2AuthorizationExchange.getAuthorizationRequest().getRedirectUri());
//        params.set("grant_type", "authorization_code");
//        String tmpTokenResponse = restTemplate.postForObject(clientRegistration.getProviderDetails().getTokenUri(), params, String.class);
//        Map<String,Object> map=new ObjectMapper().readValue(tmpTokenResponse,Map.class);
//        String accessToken = map.get("access_token")+"";
//        String refreshToken= map.get("refresh_token")+"";
//        Long expiresIn= Long.parseLong(map.get("expires_in")+"");
//        Map<String, Object> additionalParameters=new HashMap<>(1);
//        additionalParameters.put("uid",map.get("uid"));
//        log.info("client_id: "+clientRegistration.getClientId());
//        log.info("client_secret:   ",clientRegistration.getClientSecret());
//        log.info("code:  "+oAuth2AuthorizationExchange.getAuthorizationRequest().getAuthorizationRequestUri());
//        log.info("redirect_uri:   "+oAuth2AuthorizationExchange.getAuthorizationRequest().getRedirectUri());
//        log.info("access_token: ---------------------------------------------------------"+accessToken);
//        Set<String> scopes = new LinkedHashSet<>(oAuth2AuthorizationExchange.getAuthorizationRequest().getScopes());
//        OAuth2AccessToken.TokenType accessTokenType = OAuth2AccessToken.TokenType.BEARER;
//        return OAuth2AccessTokenResponse.withToken(accessToken).refreshToken(refreshToken).expiresIn(expiresIn).additionalParameters(additionalParameters).tokenType(accessTokenType).scopes(scopes).build();
//    }
//}
